package com.michaelbulava.TFS.ui.wizards.share;

import com.intellij.ide.wizard.CommitStepException;
import com.intellij.openapi.module.Module;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.table.JBTable;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import com.michaelbulava.TFS.Messages;
import com.michaelbulava.TFS.ui.framework.wizard.ExtendedWizardStep;
import com.michaelbulava.TFS.ui.wizards.common.IConnectWizard;
import com.michaelbulava.TFS.ui.wizards.common.IShareWizard;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;

/**
 * Created by MichaelA on 7/29/2015.
 */
public class ShareModulesSummaryWizardStep extends ExtendedWizardStep {
    public static final Object STEP_ID = new Object();
    private final IShareWizard wizard;
    private SummaryControl control;
    private moduleMappingTableModel model;

    public ShareModulesSummaryWizardStep(IShareWizard wizard){
        super(Messages.getString("ShareModulesSummaryWizardStep.Title"));
        this.wizard = wizard;
        model = new moduleMappingTableModel();
    }

    @Override
    public void _init(){
        super._init();
        model.fireTableDataChanged();
    }

    @Override
    public Object getStepId() {
        return STEP_ID;
    }

    @Override
    public Object getNextStepId() {
        return wizard.getNextStepId(this);
    }

    @Override
    public Object getPreviousStepId() {
        return wizard.getPreviousStepId(this);
    }

    @Override
    public void commit(CommitType commitType) throws CommitStepException {

    }

    @Override
    public boolean isComplete() {
        return true;
    }

    @Override
    public JComponent getComponent() {
        if (control == null);
            control = new SummaryControl();
        return control;
    }

    @Override
    public JComponent getPreferredFocusedComponent() {
        return null;
    }

    private class SummaryControl extends JPanel{
        private JBTable moduleMappingTable;

        public SummaryControl(){
            super();

            setLayout(new GridLayoutManager(4, 1, new Insets(4, 4, 4, 4), -1, -1));
            final JPanel panel2 = new JPanel();
            panel2.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
            add(panel2, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
                    GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                    GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0,
                    false));

            final JTextPane textPane1 = new JTextPane();
            textPane1.setText(Messages.getString("ShareModulesSummaryWizardStep.UploadConfirmationText"));
            panel2.add(textPane1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER,
                    GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_WANT_GROW,
                    GridConstraints.SIZEPOLICY_WANT_GROW, null, new Dimension(150, 50), null, 0, false));

            final Spacer spacer1 = new Spacer();
            add(spacer1, new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL,
                    1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));

            final JPanel panel3 = new JPanel();
            panel3.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
            add(panel3, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
                    GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                    GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0,
                    false));

            final JScrollPane scrollPane1 = new JBScrollPane();
            panel3.add(scrollPane1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER,
                    GridConstraints.FILL_BOTH,
                    GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW,
                    GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0,
                    false));

            moduleMappingTable = new JBTable();
            moduleMappingTable.setModel(model);
            scrollPane1.setViewportView(moduleMappingTable);
            final JLabel label1 = new JLabel();
            label1.setText(Messages.getString("ShareModulesSummaryWizardStep.SelectedProjectsLabelText"));
            add(label1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
                    GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));


        }

    }

    private class moduleMappingTableModel extends AbstractTableModel{

        @Override
        public int getRowCount() {
            return wizard.getModulesToShare().length;
        }

        @Override
        public int getColumnCount() {
            return 3;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Module module = wizard.getModulesToShare()[rowIndex];
            if (module != null){
                switch(columnIndex){
                    case 0:
                        return module.getName();
                    case 1:
                        return module.getModuleFile().getParent().getPath();
                    case 2:
                        return wizard.getModuleShareLocation(module);
                    default:
                        return null;
                }
            } else {
                return null;
            }
        }

        @Override
        public String getColumnName(int column) {
            switch(column){
                case 0:
                    return Messages.getString("ShareModulesSummaryWizardStep.ModuleColumnName");
                case 1:
                    return Messages.getString("ShareModulesSummaryWizardStep.LocalPathColumnName");
                case 2:
                    return Messages.getString("ShareModulesSummaryWizardStep.ServerPathColumnName");
                default:
                    return null;
            }
        }
    }
}
